package com.fs.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * sql数据库连接 
 * 2018/03/23
 * 数据库名称：FlowerShop
 * @author zxh
 *
 */
public class DBUtils {
	public static String username="root";
	public static String password="123456";
	public static String url="jdbc:mysql://127.0.0.1:3306/FlowerShop?useUnicode=true&characterEncoding=utf8";
	public static String driver="com.mysql.jdbc.Driver";

	static{
		try {
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
		}//加载驱动
	}

	/**
	 * 获取数据库连接
	 * @throws SQLException 
	 */
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, username, password);
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 关闭数据库连接
	 * @param con
	 * @param rs
	 * @param st
	 * @param ps
	 */
	public static void close(Connection con,ResultSet rs,Statement st,PreparedStatement ps){
		try {
			if(rs!=null){
				rs.close();
			}
			if(st!=null){
				st.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	public static void main(String[] args) {
		try {
			getConnection();
			System.out.println("成功连接数据库");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}


